package client;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import common.User;

public class InfoModifier extends JDialog implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private IMClientGUI gui;
	private JTextField aliasField;
	public InfoModifier(IMClientGUI gui){
		this.gui = gui;
		User user = gui.getUser();
		JPanel mainPane = new JPanel();
		mainPane.setLayout(new GridLayout(3,2,5,10));
		JLabel idLabel = new JLabel("ID",JLabel.RIGHT);
		JTextField idField = new JTextField(String.valueOf(user.getUserId()));
		idLabel.setLabelFor(idField);
		idField.setEditable(false);
		JLabel aliasLabel = new JLabel("Alias",JLabel.RIGHT);
		aliasField = new JTextField(user.getAlias());
		aliasLabel.setLabelFor(aliasField);
		JButton modify = new JButton("modify");
		modify.setActionCommand("modify");
		modify.addActionListener(this);
		JButton cancel = new JButton("cancel");
		cancel.setActionCommand("cancel");
		cancel.addActionListener(this);
		mainPane.add(idLabel);
		mainPane.add(idField);
		mainPane.add(aliasLabel);
		mainPane.add(aliasField);
		mainPane.add(modify);
		mainPane.add(cancel);
		mainPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		this.setContentPane(mainPane);
		this.setTitle("Info of "+user.getAlias());
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.pack();
		this.setLocationRelativeTo(gui);
		this.setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("cancel")){
			dispose();
		} else if(e.getActionCommand().equals("modify")){
			gui.changeAlias(aliasField.getText());
			dispose();
		}
		
	}
}
